package com.mutouren.web.org;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mutouren.common.entity.ValidState;
import com.mutouren.common.utils.StringUtils;
import com.mutouren.modules.org.model.App;
import com.mutouren.modules.org.model.Function;
import com.mutouren.modules.org.model.Role;
import com.mutouren.modules.org.service.AppService;
import com.mutouren.modules.org.service.FunctionService;
import com.mutouren.modules.org.service.RoleService;
import com.mutouren.web.WebUtils;

@Controller
@RequestMapping(value="/org/role")
public class RoleAction {

	@Resource
	private AppService appService;
	
	@Resource
	private RoleService roleService;
	
	@Resource
	private FunctionService functionService;	
		
	@RequestMapping(value="/manage.action")
	public String manage(@RequestParam(value="appId", required=false, defaultValue="0") int appId,
			HttpServletRequest request) {
		
		request.setAttribute("appId", appId);
		if (appId != 0) {
			App app = appService.get(appId);
			request.setAttribute("appName", app.getAppName());
		}
		
		List<Role> listRole = roleService.selectByAppId(appId);
		request.setAttribute("listRole", listRole);
		
		return "modules/org/roleList";
	}

	@RequestMapping(value="/add.action")
	public String add(@ModelAttribute("role") Role role, 
			HttpServletRequest request, HttpServletResponse response) {
		
		request.setAttribute("actionName", WebUtils.getActionName(request));
		if (!WebUtils.isPost(request)) {	
			return "modules/org/roleForm";
		}
		
		List<String> errors = new ArrayList<String>();
		if(this.validate(request, role, errors)) {
			return WebUtils.failDialog(response, WebUtils.getErrorMessage(errors));
		}
		
		roleService.add(role);
		return WebUtils.successDialog(response);
	}

	@RequestMapping(value="/modify.action")
	public String modify(@ModelAttribute("role") Role role, 
			ModelMap map,
			HttpServletRequest request, HttpServletResponse response) {	
		
		request.setAttribute("actionName", WebUtils.getActionName(request));
		if (!WebUtils.isPost(request)) {						
			role = roleService.get(role.getRoleId());			
			map.addAttribute("role", role);
			return "modules/org/roleForm";
		}
		
		List<String> errors = new ArrayList<String>();
		if(this.validate(request, role, errors)) {
			return WebUtils.failDialog(response, WebUtils.getErrorMessage(errors));
		}
		
		int result = roleService.modify(role);
		if (result == 1) {
			return WebUtils.successDialog(response);
		} else {
			return WebUtils.failDialog(response, "编辑数据不存在");
		}
	}
	
	@RequestMapping(value="/changeState.action")
	public void changeState(@RequestParam(value="roleId") int roleId, String validState,
			HttpServletResponse response) throws IOException {
		Role role = new Role();
		role.setRoleId(roleId);
		role.setValidState(ValidState.valueOf(validState));
		
		if (roleService.setState(role) == 1) {
			response.getWriter().write("success");
		} else {
			response.getWriter().write("fail");
		}
	}	

	@RequestMapping(value="/relateFunction.action")
	public String relateFunction(@RequestParam(value="roleId") int roleId,
			@RequestParam(value="relateFuncIds", required=false, defaultValue="") String relateFuncIds,
			HttpServletRequest request, HttpServletResponse response) throws IOException{

		request.setAttribute("actionName", WebUtils.getActionName(request));
		Role role = roleService.get(roleId);
		
		if (!WebUtils.isPost(request)) {		
			List<Function> listFunction = functionService.selectByAppId(role.getAppId(), false);
			List<Function> listRoleFunction = roleService.getFunctions(roleId);
			
			request.setAttribute("roleId", roleId);			
			request.setAttribute("listFunction", OrgUtils.convertFunctionToJSON(listFunction, listRoleFunction, false));
			return "modules/org/roleFunctionTree";
		}

		List<String> errors = new ArrayList<String>();
		if(this.validate(request, role, errors)) {
			return WebUtils.failDialog(response, WebUtils.getErrorMessage(errors));
		}
		
		List<Integer> listFuncId = StringUtils.toListInteger(relateFuncIds, ",");
		
		roleService.setFunctions(roleId, listFuncId);
		return WebUtils.successDialog(response);
	}
	
	@RequestMapping(value="/select.action")
	public String select(HttpServletRequest request) throws IOException{
		
		List<Role> listRole = roleService.select(null);
		request.setAttribute("listRole", listRole);

		return "modules/org/roleSelect";
	}	

	public boolean validate(HttpServletRequest request, Object model, List<String> errors ) {
		String actionName = (String) request.getAttribute("actionName");

		// role
		if (actionName.equals("add") || actionName.equals("modify")) {
			Role role = (Role) model;
			if (StringUtils.isBlank(role.getRoleName())) {
				errors.add("名称: 不能为空");
			}
		}
		
		// relateFunction
//		if (actionName.equals("relateFunction")) {
//			if (StringUtils.isEmptyOrWhitespace(request.getParameter("relateFuncIds"))) {
//				errors.add("没有选择功能");
//			}
//		}
		
		
		return errors.size() > 0;
	}		
	
}
